package com.zhentao.utils.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.green20220302.Client;
import com.aliyun.green20220302.models.TextModerationPlusRequest;
import com.aliyun.green20220302.models.TextModerationPlusResponse;
import com.aliyun.green20220302.models.TextModerationPlusResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.zhentao.model.common.dtos.ResponseResult;

/**
 * @author 86156
 * @version 1.0
 * @Date 2024/8/2114:28
 */
public class TextModerationPlusDemo {
    public static ResponseResult AutoText(String content) throws Exception {
        Config config = new Config();
        config.setAccessKeyId("LTAI5t72b53ESJ9SBfhA5nqi");
        config.setAccessKeySecret("PV3AZlUnaynkUvicuxKAYNWLJj5gAg");
        //接入区域和地址请根据实际情况修改
        config.setRegionId("cn-shanghai");
        config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
        //连接时超时时间，单位毫秒（ms）。
        config.setReadTimeout(6000);
        //读取时超时时间，单位毫秒（ms）。
        config.setConnectTimeout(3000);
        //设置http代理。
        //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
        //设置https代理。
        //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
        Client client = new Client(config);

        JSONObject serviceParameters = new JSONObject();
        serviceParameters.put("content", content);

        TextModerationPlusRequest textModerationPlusRequest = new TextModerationPlusRequest();
        // 检测类型
        textModerationPlusRequest.setService("comment_detection_pro");
        textModerationPlusRequest.setServiceParameters(serviceParameters.toJSONString());

        try {
            TextModerationPlusResponse response = client.textModerationPlus(textModerationPlusRequest);
            if (response.getStatusCode() == 200) {
                TextModerationPlusResponseBody result = response.getBody();
                System.out.println(JSON.toJSONString(result));
                System.out.println("requestId = " + result.getRequestId());
                System.out.println("code = " + result.getCode());
                System.out.println("msg = " + result.getMessage());
                Integer code = result.getCode();
                if (200 == code) {
                    TextModerationPlusResponseBody.TextModerationPlusResponseBodyData data = result.getData();
                    System.out.println(JSON.toJSONString(data, true));
                    String s = JSON.toJSONString(data, true);
                    if(s.contains("nonLabel")){
                        return ResponseResult.okResult(600,"审核成功");
                    }else{
                        for(TextModerationPlusResponseBody.TextModerationPlusResponseBodyDataResult value : data.result){
                            if(value.getConfidence()>80){
                                return ResponseResult.errorResult(601,"审核不通过");
                            } else if (value.getConfidence() < 50) {
                                return ResponseResult.okResult(600,"审核通过");
                            }else {
                                return ResponseResult.errorResult(602,"带人工审核");
                            }
                        }
                    }
                    return ResponseResult.errorResult(601,"审核不通过");
                } else {
                    System.out.println("text moderation not success. code:" + code);
                    return ResponseResult.errorResult(602,"带人工审核");
                }
            } else {
                System.out.println("response not success. status:" + response.getStatusCode());
                return ResponseResult.errorResult(602,"带人工审核");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(602,"带人工审核");
        }
    }
}
